package com.zyd.androidwifi;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class WifiZxingActivity extends AppCompatActivity {
    private ImageView ivScan;
    private TextView tvInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_zxing);
        ivScan = findViewById(R.id.ivScan);
        tvInfo = findViewById(R.id.tvInfo);
        IntentIntegrator integrator = new IntentIntegrator(this);
        ivScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(WifiZxingActivity.this, "扫一扫", Toast.LENGTH_SHORT).show();
                // 设置要扫描的条码类型 ONE_D_CODE_TYPES 一维码 QR_CODE_TYPES 二维码
                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                integrator.setPrompt("请对准二维码"); // 设置提示语
                integrator.setCameraId(0); // 选择摄像头,可使用前置或者后置
                integrator.setBeepEnabled(true); // 是否开启声音,扫完码之后会"哔"的一声
                integrator.setBarcodeImageEnabled(false); // 扫完码之后生成二维码的图片
                integrator.setOrientationLocked(false);//设置屏幕竖屏锁定
                integrator.setCaptureActivity(CustomCaptureActivity.class);//改用自定义扫码布局
                integrator.initiateScan(); // 初始化扫码
            }
        });
    }

    // 扫完码之后会在onActivityResult方法中回调结果
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() != null) {
                Log.i("zydTest", "onActivityResult:"+result.getContents());
                tvInfo.setText(result.getContents());
//                Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}